package com.zy.springboot.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.zy.springboot.com.BaseUtils;
import com.zy.springboot.com.ResponseUtils;
import com.zy.springboot.service.user.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by y on 2019/1/11.
 */
@Api(value = "用户接口", description = "用户接口")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping(value = "/getUserList.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询所有用户",
            notes = "查询所有用户 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{}", dataType = "String", paramType = "body")
    public void getUserList(HttpServletResponse response,  @RequestBody(required = false) String jsonData) {
        System.out.println(jsonData+"111");
        JSONObject records=userService.getUserList(jsonData);
        if (null != records) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(records));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("查找失败"));
        }
    }

    @ResponseBody
    @RequestMapping(value = "/getUserById.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "查询单个用户",
            notes = "查询单个用户 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userId:'1'}", dataType = "String", paramType = "body")
    public void getUserById(HttpServletResponse response,  @RequestBody String jsonData) {
        JSONObject result=userService.getUserById(jsonData);
        if (null != result) {
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjHourRespond(result));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("查找失败"));
        }
    }

    @ResponseBody
    @RequestMapping(value = "/add.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "添加用户",
            notes = "添加用户 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userName:'卡特',password:'1234321',sex:'1'}", dataType = "String", paramType = "body")
    public void add(HttpServletResponse response,  @RequestBody String jsonData) {
        JSONObject jsonResult = new JSONObject();
        jsonResult.put("id",userService.add(jsonData));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonResult));
    }

    @ResponseBody
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "修改用户",
            notes = "修改用户 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userName:'sxjczy0448',email:'1234321',introduce:'是我',id:'1'}", dataType = "String", paramType = "body")
    public void update(HttpServletResponse response,  @RequestBody String jsonData) {
        String result=userService.update(jsonData);
        ResponseUtils.putJsonResponse(response,result);
    }
    @ResponseBody
    @RequestMapping(value = "/login.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "用户登陆",
            notes = "用户登陆 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{userName:'sxjczy',password:'123456'}", dataType = "String", paramType = "body")
    public void login(HttpServletResponse response,  @RequestBody String jsonData) {
        String result=userService.login(jsonData);
        ResponseUtils.putJsonResponse(response, result);
    }

    @ResponseBody
    @RequestMapping(value = "/resetPassword.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "重置密码",
            notes = "重置密码 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{id:'1',password:'12345678',password2:'12345678',oldPassword:'123456'}", dataType = "String", paramType = "body")
    public void resetPassword(HttpServletResponse response,  @RequestBody String jsonData) {
        String result=userService.resetPassword(jsonData);
        ResponseUtils.putJsonResponse(response, result);
    }

    @ResponseBody
    @RequestMapping(value = "/sendSms.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "获取验证码",
            notes = "获取验证码 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{tel:''}", dataType = "String", paramType = "body")
    public void sendSms(HttpServletResponse response,  @RequestBody String jsonData) {
        String result=userService.sendSms(jsonData);
        JSONObject object=new JSONObject();
        object.put("flag",result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(object));
    }

    @ResponseBody
    @RequestMapping(value = "/yzmCheck.json", method = RequestMethod.POST)
    @ApiOperation(
            value = "验证码验证",
            notes = "验证码验证 ",
            httpMethod = "POST",
            produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParam(name = "jsonData", value = "{tel:'',yzm:''}", dataType = "String", paramType = "body")
    public void yzmCheck(HttpServletResponse response,  @RequestBody String jsonData) {
        String result=userService.yzmCheck(jsonData);
        JSONObject object=new JSONObject();
        object.put("flag",result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(object));
    }
}
